package com.beadhouse.nurlevel.support.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.entity.NurContentEntity;
import com.beadhouse.entity.NurLevelAndNurContentEntity;
import com.beadhouse.nurlevel.ao.AddNurLevelContentAO;
import com.beadhouse.nurlevel.ao.UpdateNurLevelContentAO;
import com.beadhouse.nurlevel.dao.NurlevelContentDao;
import com.beadhouse.nurlevel.service.NurLevelContentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Service
public class NurLevelContentServiceImpl implements NurLevelContentService{
	NurlevelContentDao nlcdao;
	public NurLevelContentServiceImpl(NurlevelContentDao nlcdao) {
		this.nlcdao=nlcdao;
	}
	
	@Override
	public void updateNurLevelContent(UpdateNurLevelContentAO ao) throws Exception {
		int levelId=ao.getLevelId();
		int contentId=ao.getContentId();
		Integer times=ao.getNurTimes();
		String cycle=ao.getNurCycle();
		NurLevelAndNurContentEntity rows=nlcdao.selectOne(levelId, contentId);
		Map<String,Object> map=new HashMap();
		if(times!=null) {
			map.put("nurTime", times);
		}
		if(cycle!=null) {
				map.put("nurCycle", cycle);
			
		}
		map.put("nurcontentId ",contentId);
		map.put("nurlevelId", levelId);
		nlcdao.update(contentId, map);
	}
	
	@Override
	public void deleteNurLevelContent(int id) throws Exception {
		nlcdao.delete(id);
		
	}
	@Override
	public List<NurContentEntity> selectAllContent() throws Exception {
		List<NurContentEntity> result=nlcdao.selectAllContent();
		return result;
	}
	
	@Override
	public void addNurLevelContent(AddNurLevelContentAO ao) throws Exception {
		NurLevelAndNurContentEntity nlc=new NurLevelAndNurContentEntity();
		nlc.setNurcontentId(ao.getContentId());
		nlc.setNurCycle(ao.getNurCycle());
		nlc.setNurlevelId(ao.getLevelId());
		nlc.setNurTimes(ao.getNurTimes());
		nlcdao.insert(nlc);
	}
	public NurLevelAndNurContentEntity selectOne(int levelId,int contentId)throws Exception{
		NurLevelAndNurContentEntity result=nlcdao.selectOne(levelId, contentId);
		return result;
	}
	
	@Override
	public QueryResult<NurLevelAndNurContentEntity> selectNurLevelContent(Integer id ) throws Exception {
		//PageHelper.startPage(1,5);
		List<NurLevelAndNurContentEntity> rows= nlcdao.selectList(id);
		PageInfo<NurLevelAndNurContentEntity> page = new PageInfo<>(rows);
		QueryResult<NurLevelAndNurContentEntity> result=new QueryResult<NurLevelAndNurContentEntity>();
		result.setPageNo(1);
		result.setPageSize(5);
		result.setRows(rows);
		result.setTotalRows((int)page.getTotal());
		return result;
		
	}
}
